/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import React from 'react';
import { observer } from 'mobx-react';
import { Table, Modal, message } from 'antd';
import ComForm from './Form';
import { http } from 'libs';
import store from './store';

@observer
class ComTable extends React.Component {
    action_name;
  componentDidMount() {
    store.fetchRecords()
  }

  handleDisable = (text, status) => {
    this.action_name = ['上线', '下线']
    Modal.confirm({
      title: `应用${this.action_name[status]}确认`,
      content: `确定要${this.action_name[status]}【${text['name']}】?`,
      onOk: () => {
        return http.options('/api/message/push/', {params: {id: text.id, status: status}})
          .then(() => {
            message.success(`应用${this.action_name[status]}成功`);
            store.fetchRecords()
          })
      }
    })
  };

  render() {
    let data = store.permRecords;
    if (store.f_name) {
      data = data.filter(item => item['name'].toLowerCase().includes(store.f_name.toLowerCase()))
    }
    if (store.f_messageservice_id) {
      data = data.filter(item => item['register_id'] === store.f_messageservice_id)
    }
    return (
      <React.Fragment>
        <Table
          rowKey="id"
          loading={store.isFetching}
          dataSource={data}
          pagination={{
            showSizeChanger: true,
            showLessItems: false,
            hideOnSinglePage: false,
            showTotal: total => `共 ${total} 条`,
            pageSizeOptions: ['10', '20', '50', '100']
          }}>
          <Table.Column width={100} title="消息服务" dataIndex="messageservice_id" render={(id) => store.messageserviceMap[id] }/>
          <Table.Column width={200} title="消息内容" dataIndex="content" />
          <Table.Column width={100} ellipsis title="备注信息" dataIndex="desc"/>
        </Table>
        {store.formVisible && <ComForm/>}
      </React.Fragment>
    )
  }
}

export default ComTable
